using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

public partial class forums_controls_forums_breadcrumb : Controls.DataManagerForumControl
{
    protected override void ItemChanged()
    {
        if (UsePostBack == true)
        {
            rptBreadcrumbPostback.DataSource = DAL.Forums.GetBreadcrumb(CurrentForum);
            rptBreadcrumbPostback.DataBind();
        }
        else
        {
            rptBreadcrumbLink.DataSource = DAL.Forums.GetBreadcrumb(CurrentForum);
            rptBreadcrumbLink.DataBind();
        }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            RefreshLinks();
        }
    }

    public void RefreshLinks()
    {
        btnRoot.Text = RootItemText;
        lnkRoot.Text = RootItemText;
        lnkRoot.NavigateUrl = RootItemUrl;
    }

    [Category("Behavior"), DefaultValue(">")]
    public string Seperator
    {
        get
        {
            if (ViewState["Seperator"] != null)
            {
                return ViewState["Seperator"].ToString();
            }
            else
            {
                return null;
            }
        }
        set
        {
            ViewState["Seperator"] = value;
            RefreshLinks();
        }
    }

    [Category("Behavior"), DefaultValue("(root)")]
    public string RootItemText
    {
        get
        {
            if (ViewState["RootItemText"] != null)
            {
                return ViewState["RootItemText"].ToString();
            }
            else
            {
                return "(root)";
            }
        }
        set
        {
            ViewState["RootItemText"] = value;
        }
    }

    [Category("Behavior")]
    public string RootItemUrl
    {
        get
        {
            if (ViewState["RootItemUrl"] != null)
            {
                return ViewState["RootItemUrl"].ToString();
            }
            else
            {
                return null;
            }
        }
        set
        {
            ViewState["RootItemUrl"] = value;
        }
    }

    [Category("Behavior"), DefaultValue(true)]
    public bool UsePostBack
    {
        get
        {
            if (ViewState["UsePostBack"] != null)
            {
                return Convert.ToBoolean(ViewState["UsePostBack"]);
            }
            else
            {
                return true;
            }
        }
        set
        {
            ViewState["UsePostBack"] = value;
            if (value == true)
            {
                mviewBreadcrumb.SetActiveView(viewUsePostback);
            }
            else
            {
                mviewBreadcrumb.SetActiveView(viewUseLink);
            }
        }
    }

    [Category("Behavior"), Description("Example: '~/forums/thread.aspx?t={0}'")]
    public string NavigateUrlFormat
    {
        get
        {
            if (ViewState["NavigateUrlFormat"] != null)
            {
                return ViewState["NavigateUrlFormat"].ToString();
            }
            else
            {
                return null;
            }
        }
        set
        {
            ViewState["NavigateUrlFormat"] = value;
        }
    }

    [Category("Behavior")]
    public string FooterText
    {
        get
        {
            if (ViewState["FooterText"] != null)
            {
                return ViewState["FooterText"].ToString();
            }
            else
            {
                return null;
            }
        }
        set
        {
            ViewState["FooterText"] = value;
        }
    }

    [Category("Behavior")]
    public string FooterUrl
    {
        get
        {
            if (ViewState["FooterUrl"] != null)
            {
                return ViewState["FooterUrl"].ToString();
            }
            else
            {
                return null;
            }
        }
        set
        {
            ViewState["FooterUrl"] = value;
        }
    }


    protected string GetNavigateUrl(object o)
    {
        if (o != null)
        {
            long forumID = 0;
            if (Int64.TryParse(o.ToString(), out forumID) && NavigateUrlFormat.Contains("{0}"))
            {
                return string.Format(NavigateUrlFormat, forumID);
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    }

    protected void NewForumSelected(object sender, CommandEventArgs e)
    {
        if (String.IsNullOrEmpty(e.CommandArgument.ToString()))
        {
            CurrentForum = null;
        }
        else
        {
            long forumID = 0;
            if (Int64.TryParse(e.CommandArgument.ToString(), out forumID))
            {
                CurrentForum = DAL.Forums.Item(forumID);
            }
            else
            {
                CurrentForum = null;
            }
        }

        RaiseForumSelected();
    }
}
